const express = require('express');
const router = express.Router();

/**
 * @api {post} /api/user/login 用户登录
 * @apiDescription 输入账号密码登录系统
 * @apiName Login
 * @apiGroup User
 * @apiBody {String} userName 用户名
 * @apiBody {String} passWord 密码
 * @apiSuccess {json} result
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code": "200",
 *      "success" : "true",
 *      "data" : {
 *          "name" : "userName",
 *          "pass" : "passWord",
 *          "sex": "0",
 *          "age": "18"
 *      }
 *  }
 * @apiSampleRequest http://localhost:3001/api/user/login
 * @apiVersion 0.1.0
 */
router.post('/login', (req, res, next) => {
    console.log(req.body);
    let username = req.body.username;
    let password = req.body.password;

    // 设置cookie
    // res.cookie('username', userName, {httpOnly: true, path: '/', maxAge: 10000});

    if (username === 'admin' && password === '123456') {
        res.json({
            code: 200,
            success: true,
            data: {
                token: 'NiuAdmin.Administrator.Auth',
                id: 1,
                name: '小笑残虹'
            }
        })
    } else {
        res.writeHead(400, {
            'Content-Type': 'text/plain; charset=utf-8;'
        });
        res.end('用户名密码不正确');
    }


});

/**
 * @api {post} /api/user/register 用户注册
 * @apiDescription 设置账号密码注册系统账户
 * @apiName Register
 * @apiGroup User
 * @apiBody {String} userName 用户名
 * @apiBody {String} passWord 密码
 * @apiSuccess {json} result
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code": "200",
 *      "success" : "true",
 *      "data" : {
 *          "msg" : "register ok",
 *      }
 *  }
 * @apiSampleRequest http://localhost:3001/api/user/register
 * @apiVersion 0.1.0
 */
router.post('/register', (req, res, next) => {
    let userName = req.body.userName;
    let passWord = req.body.passWord;

    res.json({
        code: 200,
        success: true,
        data: {
            msg: 'register ok'
        }
    });
});

/**
 * @api {get} /api/user/userinfo 获取用户信息
 * @apiDescription 根据用户ID获取用户信息
 * @apiName Userinfo
 * @apiGroup User
 * @apiParam {String} id 用户id
 * @apiSuccess {json} result
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code": "200",
 *      "success" : "true",
 *      "data" : {
 *          "id": "userId",
            name: '小笑残虹',
            sex: 0,
            age: 18
 *      }
 *  }
 * @apiSampleRequest http://localhost:3001/api/user/userinfo
 * @apiVersion 0.1.0
 */
router.get('/userinfo', (req, res, next) => {
    // console.log('cookies', req.cookies);
    let {id} = req.query;

    if (id == 1) {
        res.json({
            code: 200,
            success: true,
            data: {
                id,
                name: '小笑残虹',
                sex: 0,
                age: 18
            }
        });
    } else {
        res.writeHead(400, {
            'Content-Type': 'text/plain; charset=utf-8;'
        });
        res.end('没找到该用户');
    }


});

module.exports = router;